package com.ch04.ex38;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * Created by zc on 2016/2/18.
 */
public class Task implements Callable<Result> {
    private String name;

    public Task(String name) {
        this.name = name;
    }

    @Override
    public Result call() throws Exception {
        System.out.printf("%s: Starting\n", this.name);
        long duration = (long) (Math.random()*10);
        System.out.printf("%s: Waiting %d seconds for result.\n",
                name,duration);
        TimeUnit.SECONDS.sleep(duration);

        int value = 0;
        for(int i=0; i<5; i++){
            value += Math.random()*100;
        }

        Result result = new Result();
        result.setName(name);
        result.setValue(value);

        System.out.println(name + ": Ends");

        return result;
    }
}
